<script  setup >
import { reactive, ref } from 'vue'
import { Form, Field } from 'vee-validate'
import validate from '@/utils/validate'
import Message from '@/components/Message'
import { useStore } from 'vuex'
import { getUserCodeAPI } from '@/api'
import { useIntervalFn } from '@vueuse/core'
import { useRouter } from 'vue-router'
// const instance = getCurrentInstance()

const isMsgLogin = ref(false)
// const isAgree = ref(false)
const loginForm = reactive({
  account: 'zhousg',
  password: '123456',
  mobile: '13241051259',
  code: '',
  isAgree: true
})

const schema = {
  account: validate.account,
  isAgree: validate.isAgree,
  password: validate.password,
  mobile: validate.moblie,
  code: validate.code

}

const router = useRouter()
const store = useStore()
const target = ref(null)
const loginFn = () => {
  target.value.validate().then(async res => {
    console.log('手动兜底校验', res)
    if (res) {
      // Message({ type: 'success', text: '登录成功' })
      if (isMsgLogin.value) {
        // 短信登录
        try {
          await store.dispatch('user/getUserMoblieAction', { mobile: loginForm.mobile, code: loginForm.code })
          Message({ type: 'success', text: '登录成功' })
          router.push('/')
        } catch (error) {
          Message({ type: 'error', text: error.message })
        }
      } else {
        // 用户名登录
        try {
          await store.dispatch('user/getUserAccountAction', { account: loginForm.account, password: loginForm.password })
          Message({ type: 'success', text: '登录成功' })
          router.push('/')
        } catch (error) {
          Message({ type: 'error', text: error.message })
        }
      }
    }
  })
}

// pause 暂停 resume 开始
// useIntervalFn(回调函数,执行间隔,是否立即开启)
const time = ref(0)
const { pause, resume } = useIntervalFn(() => {
  time.value--
  if (time.value <= 0) {
    pause()
  }
}, 1000, { immediate: false })

const start = (num) => {
  time.value = num
  resume()
}
const sendFn = async () => {
  console.log(111)
  try {
    if (validate.moblie(loginForm.mobile) === true) {
      if (time.value === 0) {
        await getUserCodeAPI(loginForm.mobile)
        Message({ type: 'success', text: '发送短信验证码成功' })
        start(60)
      }
    } else {
      Message({ type: 'error', text: '手机号格式不对' })
    }
  } catch (error) {
    Message({ type: 'error', text: error.response.data.message })
  }
}

// 初始化QQ登录按钮 （官方）
// 1. 准备span有id = qqLoginBtn
// 2. QC.Login({btnId:"qqLoginBtn"})
// onMounted(() => {
//   QC.Login({ btnId: 'qqLoginBtn' })
// })
const appId = process.env.VUE_APP_QQAPPID
const qqBack = encodeURIComponent(process.env.VUE_APP_QQBACK)
const qqUrl = `https://graph.qq.com/oauth2.0/authorize?client_id=${appId}&response_type=token&scope=all&redirect_uri=${qqBack}`

</script>
<template>
  <div class="account-box">
    <!-- <message :type="'success'" :text="'测试'"></message> -->
    <div class="toggle">
      <a href="javascript:;" v-if="isMsgLogin" @click="isMsgLogin=false">
        <i class="iconfont icon-user"></i> 使用账号登录
      </a>
      <a href="javascript:;" v-else @click="isMsgLogin=true">
        <i class="iconfont icon-msg"></i> 使用短信登录
      </a>
    </div>
    <Form class="form" ref="target" :validation-schema="schema" v-slot="{errors}" >
      <template v-if="!isMsgLogin">

        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field type="text" name="account" placeholder="请输入用户名或手机号" v-model="loginForm.account" :class="{error:errors.account}"/>
          </div>
          <div class="error" v-if="errors.account"><i class="iconfont icon-warning" />{{ errors.account }}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-lock"></i>
            <Field type="password" name="password" placeholder="请输入密码"  v-model="loginForm.password" :class="{error:errors.password}"/>
          </div>
          <div class="error" v-if="errors.password"><i class="iconfont icon-warning" />{{ errors.password }}</div>
        </div>

      </template>
      <template v-else>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-user"></i>
            <Field type="text" name="mobile" placeholder="请输入手机号" v-model="loginForm.mobile" :class="{error:errors.mobile}"/>
          </div>
          <div class="error" v-if="errors.mobile"><i class="iconfont icon-warning" />{{ errors.mobile }}</div>
        </div>
        <div class="form-item">
          <div class="input">
            <i class="iconfont icon-code"></i>
            <Field type="text" name="code" placeholder="请输入验证码"  v-model="loginForm.code" :class="{error:errors.code}"/>
            <span class="code" @click="sendFn">{{ time===0?'发送验证码':`${time}秒后` }}</span>
          </div>
          <div class="error" v-if="errors.code"><i class="iconfont icon-warning" />{{ errors.code }}</div>
        </div>
      </template>
      <div class="form-item">
        <div class="agree">
          <checkbox v-model="loginForm.isAgree"/>
            <span>我已同意</span>
            <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>

        </div>

      </div>
      <a href="javascript:;" class="btn" @click="loginFn">登录</a>
    </Form>
    <div class="action">
      <a :href="qqUrl">
        <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
      </a>
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 40px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      cursor: pointer;
    }
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
